package com.xu.actdlg;

import java.util.ArrayList;
import java.util.List;

import com.xu.Log.AppLog;
import com.xu.Log.Log;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

public abstract class DlgShower implements DialogCaseListenor{
	private Context context;
	private static List<DlgShower> dlgShowerList=new ArrayList<DlgShower>();

	private String dlgID;
	private boolean canCancel=true;

	private static Log<String> appLog = new AppLog(3, DlgShower.class.getName());

	
	public DlgShower(Context context) {
		super();
		this.dlgID = System.currentTimeMillis() + "" + Math.random() * 100;
		this.context = context;
	}



	private Intent getShowActDlgIntent(String dlgActClassName){
		

			Intent intent = new Intent();
			
			
			ComponentName component = new ComponentName(context,dlgActClassName);
			intent.setComponent(component);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra("dlgID", dlgID);
			
			return intent;
	}
	
	
	public void show(Class <?> dlgClass,String dlgTitle){
		Intent intent = getShowActDlgIntent(dlgClass.getName());
		intent.putExtra("dlgTitle", dlgTitle);
		intent.putExtra("canCancel", canCancel);
		context.startActivity(intent);
		dlgShowerList.add(this);
	}
	
	protected DialogCaseListenor bindDlg=null;
	
	
	public DialogCaseListenor getBindDlg() {
		return bindDlg;
	}



	public void setBindDlg(DialogCaseListenor bindDlg) {
		this.bindDlg = bindDlg;
	}


	@Override
	public void dismiss(){
		if(bindDlg!=null){
			bindDlg.dismiss();
		}
	}
	
	public static DlgShower getDlgShowerByID(String taskId){
		for (int i = 0; i < dlgShowerList.size(); ++i) {
			String id = dlgShowerList.get(i).getDlgID();
			if (taskId.equals(id)) {
				DlgShower result=dlgShowerList.get(i);
				return result;
			}
		}
		return null;
	}
	

	public synchronized static DlgShower regiserActivity(DialogCaseListenor dlg) {
		if (dlg == null||dlg.getDlgID()==null)
			return null;

		 DlgShower target=getDlgShowerByID(dlg.getDlgID());
		 target.setBindDlg(dlg);
		 return target;
	}

	public synchronized static DlgShower unRegiserActivity(DialogCaseListenor dlg) {
		if (dlg == null||dlg.getDlgID()==null)
			return null;
		try {
			 dlgShowerList.remove(getDlgShowerByID(dlg.getDlgID()));
		} catch (Exception e) {
			appLog.e(e.toString());
		}
		return null;

	}
	
	

	public boolean isCanCancel() {
		return canCancel;
	}



	public void setCanCancel(boolean canCancel) {
		this.canCancel = canCancel;
	}



	@Override
	public String getDlgID() {
		return dlgID;
	}
	
	
}

